/**
 * AspectSense project
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, started at the University of Cyprus
 * [http://www.ucy.ac.cy].
 *
 * More information about the project is available at
 * [http://code.google.com/p/aspectsense]
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.aspectsense.example_apps.sorted_helloworld;

import org.aspectsense.annotations.Component;
import org.aspectsense.annotations.Service;
import org.aspectsense.annotations.ServiceUtility;
import org.aspectsense.framework.DefaultComponent;
import org.aspectsense.framework.Stateful;

import java.io.Serializable;
import java.util.Stack;

/**
 * <p/>
 * Date: Sep 10, 2007<br/>
 * Time: 6:50:32 PM
 * </p>
 *
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 */
@Component(
        displayID = "StackServiceImpl"
)
@Service(
        serviceType = StackService.class
)
@ServiceUtility(
        targetServiceType = StackService.class,
        utilityFunction = "context[context.infrastructure.memory-availability] > 0.5 ? 0.7 : 0.3"
)
public class StackServiceImpl extends DefaultComponent implements StackService, Stateful, Serializable
{
    private java.util.Stack<Object> stack = new java.util.Stack<Object>();

    public void push(final Object item)
    {
        stack.push(item);
    }

    public Object pop()
    {
        return stack.pop();
    }

    // implementing the Stateful interface

    public Object getState()
    {
        return stack;
    }

    public Object setState(Object state)
    {
        final Object oldStack = stack;
        final Stack newStack = (Stack) state;

        // create a new instance of the state
        stack = new Stack<Object>();
        // copy values from the given state
        while(!newStack.isEmpty())
        {
            stack.push(newStack.remove(0));
        }

        return oldStack;
    }

    // end of implementing the Stateful interface
}